package com.suyang.syt.util;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ReportsUtil {
	private ReportsUtil() {

	}

	/**
	 * 分析月报表开始时间和结束时间，返回正确的开始时间和结束时间
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static Map<String, String> monthAnalysisDate(String startDate, String endDate) throws ParseException {
		Map<String, String> map = new HashMap<String, String>();
		String now = DateFormat.dateToString(new Date(), "yyyy-MM-dd");
		String start = "";
		String end = "";
		String lastYearStart = "";
		String lastYearEnd = "";
		String lastMonthStart = "";
		String lastMonthEnd = "";
		if (EmptyUtil.isNotEmpty(startDate) && EmptyUtil.isNotEmpty(endDate)) {
			// 开始时间和结束时间都不为空
			if (DateFormat.compareDate(startDate, endDate)) {
				return null;
			}
			start = startDate;
			end = endDate;
			lastYearStart = Integer.parseInt(start.substring(0, 4)) - 1 + start.substring(4);
			lastYearEnd = Integer.parseInt(end.substring(0, 4)) - 1 + end.substring(4);
			lastMonthStart = getLastMonthStartDay(start.substring(0, 4), start.substring(5, 7)).substring(0, 7) + start.substring(7);
			lastMonthEnd = getLastMonthStartDay(end.substring(0, 4), end.substring(5, 7)).substring(0, 7) + end.substring(7);
		} else {
			if (EmptyUtil.isEmpty(startDate) && EmptyUtil.isNotEmpty(endDate)) {
				// 开始时间为空、结束时间不为空的时候,取结束时间的当月1号到结束时间
				start = endDate.substring(0, 8) + "01";
				end = endDate;
				lastYearStart = Integer.parseInt(start.substring(0, 4)) - 1 + start.substring(4);
				lastYearEnd = lastYearStart.substring(0, 8) + end.substring(8);
				lastMonthStart = getLastMonthStartDay(start.substring(0, 4), start.substring(5, 7));
				lastMonthEnd = lastMonthStart.substring(0, 8) + end.substring(8);
			} else if (EmptyUtil.isNotEmpty(startDate) && EmptyUtil.isEmpty(endDate)) {
				// 开始时间不为空、结束时间为空的时候，取开始时间到开始时间当月的最后一天
				start = startDate;
				end = getLastEndDay(start.substring(0, 4), start.substring(5, 7));
				lastYearStart = Integer.parseInt(start.substring(0, 4)) - 1 + start.substring(4);
				lastYearEnd = getLastEndDay(lastYearStart.substring(0, 4), lastYearStart.substring(5, 7));
				lastMonthStart = getLastMonthStartDay(start.substring(0, 4), start.substring(5, 7)).substring(0, 8) + start.substring(8);
				lastMonthEnd = getLastEndDay(lastMonthStart.substring(0, 4), lastMonthStart.substring(5, 7));
			} else {
				// 开始时间和结束时间都为空的时候，取当月
				start = now.substring(0, 8) + "01";
				end = getLastEndDay(start.substring(0, 4), start.substring(5, 7));
				lastYearStart = Integer.parseInt(start.substring(0, 4)) - 1 + start.substring(4);
				lastYearEnd = getLastEndDay(lastYearStart.substring(0, 4), lastYearStart.substring(5, 7));
				lastMonthStart = getLastMonthStartDay(start.substring(0, 4), start.substring(5, 7)).substring(0, 8) + start.substring(8);
				lastMonthEnd = getLastEndDay(lastMonthStart.substring(0, 4), lastMonthStart.substring(5, 7));
			}

		}
		map.put("start", start + " 00:00:00");
		map.put("end", end + " 23:59:59");
		map.put("lastYearStart", lastYearStart + " 00:00:00");
		map.put("lastYearEnd", lastYearEnd + " 23:59:59");
		map.put("lastMonthStart", lastMonthStart + " 00:00:00");
		map.put("lastMonthEnd", lastMonthEnd + " 23:59:59");
		return map;
	}

	/**
	 * 根据传入的年和月得到当月的最后一天
	 * 
	 * @param yyyy
	 * @param mm
	 * @return
	 */
	private static String getLastEndDay(String yyyy, String mm) {
		String endDate = "";
		if (EmptyUtil.isNotEmpty(yyyy) && EmptyUtil.isNotEmpty(mm)) {
			endDate = yyyy + "-" + mm + "-";
			if ("01".equals(mm) || "03".equals(mm) || "05".equals(mm) || "07".equals(mm) || "08".equals(mm) || "10".equals(mm) || "12".equals(mm)) {
				endDate += "31";
			} else if ("04".equals(mm) || "06".equals(mm) || "09".equals(mm) || "11".equals(mm)) {
				endDate += "30";
			} else if ("02".equals(mm)) {
				int year = Integer.parseInt(yyyy);
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					endDate += "29";
				} else {
					endDate += "28";
				}
			} else {
				return "";
			}
		}
		return endDate;
	}

	/**
	 * 根据传入的年和月得到上个月的第一天
	 * 
	 * @param yyyy
	 * @param mm
	 * @return
	 */
	private static String getLastMonthStartDay(String yyyy, String mm) {
		String startDate = "";
		if (EmptyUtil.isNotEmpty(yyyy) && EmptyUtil.isNotEmpty(mm)) {
			if ("01".equals(mm)) {
				startDate = Integer.parseInt(yyyy) - 1 + "-12-01";
			} else if ("02".equals(mm)) {
				startDate = yyyy + "-01-01";
			} else if ("03".equals(mm)) {
				startDate = yyyy + "-02-01";
			} else if ("04".equals(mm)) {
				startDate = yyyy + "-03-01";
			} else if ("05".equals(mm)) {
				startDate = yyyy + "-04-01";
			} else if ("06".equals(mm)) {
				startDate = yyyy + "-05-01";
			} else if ("07".equals(mm)) {
				startDate = yyyy + "-06-01";
			} else if ("08".equals(mm)) {
				startDate = yyyy + "-07-01";
			} else if ("09".equals(mm)) {
				startDate = yyyy + "-08-01";
			} else if ("10".equals(mm)) {
				startDate = yyyy + "-09-01";
			} else if ("11".equals(mm)) {
				startDate = yyyy + "-10-01";
			} else if ("12".equals(mm)) {
				startDate = yyyy + "-11-01";
			}
		}
		return startDate;
	}

	/**
	 * 分析统计图形开始时间和结束时间，返回正确的开始时间和结束时间
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static Map<String, String> analysisDate(String startDate, String endDate) throws ParseException {
		Map<String, String> map = new HashMap<String, String>();
		String now = DateFormat.dateToString(new Date(), "yyyy-MM-dd");
		String start = "";
		String end = "";
		if (EmptyUtil.isNotEmpty(startDate) && EmptyUtil.isNotEmpty(endDate)) {
			// 开始时间和结束时间都不为空
			if (DateFormat.compareDate(startDate, endDate)) {
				return null;
			}
			start = startDate;
			end = endDate;
		} else {
			if (EmptyUtil.isEmpty(startDate) && EmptyUtil.isNotEmpty(endDate)) {
				// 开始时间为空、结束时间不为空的时候,取结束时间的当月1号到结束时间
				start = endDate.substring(0, 8) + "01";
				end = endDate;
			} else if (EmptyUtil.isNotEmpty(startDate) && EmptyUtil.isEmpty(endDate)) {
				// 开始时间不为空、结束时间为空的时候，取开始时间到开始时间当月的最后一天
				start = startDate;
				end = getLastEndDay(start.substring(0, 4), start.substring(5, 7));
			} else {
				// 开始时间和结束时间都为空的时候，取当月
				start = now.substring(0, 8) + "01";
				end = getLastEndDay(start.substring(0, 4), start.substring(5, 7));
			}
		}
		map.put("start", start + " 00:00:00");
		map.put("end", end + " 23:59:59");
		return map;
	}

	/**
	 * 分析季度报表开始时间和结束时间，返回正确的开始时间和结束时间
	 * 
	 * @param year
	 * @param quarter
	 * @return
	 * @throws NumberFormatException
	 * @throws ParseException
	 */
	public static Map<String, String> quarterAnalysisDate(String year, String quarter) throws NumberFormatException, ParseException {
		// 定义季度开始时间
		String oneQuarter = "-12-25";
		String twoQuarter = "-03-25";
		String thrQuarter = "-06-25";
		String fouQuarter = "-09-25";

		String start = "";
		String end = "";
		String lastQuarterStart = "";
		String lastQuarterEnd = "";
		String lastYearStart = "";
		String lastYearEnd = "";
		Date date = new Date();

		// 如果年度为空取今年
		if (EmptyUtil.isEmpty(year)) {
			year = DateFormat.dateToString(date, "yyyy");
		}
		// 如果季度为空取当前季度
		if (EmptyUtil.isEmpty(quarter)) {
			if (date.getTime() >= DateFormat.stringToDate(Integer.parseInt(year) - 1 + oneQuarter, "yyyy-MM-dd").getTime() && date.getTime() < DateFormat.stringToDate(year + twoQuarter, "yyyy-MM-dd").getTime()) {
				quarter = "1";
			} else if (date.getTime() >= DateFormat.stringToDate(year + twoQuarter, "yyyy-MM-dd").getTime() && date.getTime() < DateFormat.stringToDate(year + thrQuarter, "yyyy-MM-dd").getTime()) {
				quarter = "2";
			} else if (date.getTime() >= DateFormat.stringToDate(year + thrQuarter, "yyyy-MM-dd").getTime() && date.getTime() < DateFormat.stringToDate(year + fouQuarter, "yyyy-MM-dd").getTime()) {
				quarter = "3";
			} else if (date.getTime() >= DateFormat.stringToDate(year + fouQuarter, "yyyy-MM-dd").getTime() && date.getTime() < DateFormat.stringToDate(year + oneQuarter, "yyyy-MM-dd").getTime()) {
				quarter = "4";
			}
		}

		// 生成当前报表时间
		if ("1".equals(quarter)) {
			start = Integer.parseInt(year) - 1 + oneQuarter;
			end = year + twoQuarter;
			lastYearStart = Integer.parseInt(year) - 2 + oneQuarter;
			lastYearEnd = Integer.parseInt(year) - 1 + twoQuarter;
			lastQuarterStart = Integer.parseInt(year) - 1 + fouQuarter;
			lastQuarterEnd = Integer.parseInt(year) - 1 + oneQuarter;
		} else if ("2".equals(quarter)) {
			start = year + twoQuarter;
			end = year + thrQuarter;
			lastYearStart = Integer.parseInt(year) - 1 + twoQuarter;
			lastYearEnd = Integer.parseInt(year) - 1 + thrQuarter;
			lastQuarterStart = Integer.parseInt(year) - 1 + oneQuarter;
			lastQuarterEnd = year + twoQuarter;
		} else if ("3".equals(quarter)) {
			start = year + thrQuarter;
			end = year + fouQuarter;
			lastYearStart = Integer.parseInt(year) - 1 + thrQuarter;
			lastYearEnd = Integer.parseInt(year) - 1 + fouQuarter;
			lastQuarterStart = year + twoQuarter;
			lastQuarterEnd = year + thrQuarter;
		} else if ("4".equals(quarter)) {
			start = year + fouQuarter;
			end = Integer.parseInt(year) + 1 + oneQuarter;
			lastYearStart = Integer.parseInt(year) - 1 + fouQuarter;
			lastYearEnd = Integer.parseInt(year) + oneQuarter;
			lastQuarterStart = Integer.parseInt(year) + thrQuarter;
			lastQuarterEnd = Integer.parseInt(year) + fouQuarter;
		} else {
			return null;
		}

		Map<String, String> map = new HashMap<String, String>();
		map.put("start", start + " 00:00:00");
		map.put("end", end + " 00:00:00");
		map.put("lastYearStart", lastYearStart + " 00:00:00");
		map.put("lastYearEnd", lastYearEnd + " 00:00:00");
		map.put("lastQuarterStart", lastQuarterStart + " 00:00:00");
		map.put("lastQuarterEnd", lastQuarterEnd + " 00:00:00");
		map.put("year", year);
		map.put("quarter", quarter);
		return map;
	}

}
